package hxg.java2022.javabasics;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author HeXiangGuang
 * @create 2022-07-18 10:10
 */
public class LambdaDemo {
    public static void main(String[] args) {
       /* *//**
         *接口调用(方式一)
         *//*
        IEat iEat = new IEatimpl();
        iEat.eat(null,null);
        *//**
         *匿名内部类调用(方式二)
         *//*
        IEat iEat2 = new IEat() {
            @Override
            public void eat(String str,String name) {
                System.out.println("eat banana");
            }
        };
        iEat2.eat(null,null);*/
        /**
         * Lambda表达式（方式三）
         * 1.好处代码简洁，2.不会单独生成class文件3.参数类型可以省略
         * 4.可以带返回值5.参数中要加类似final修饰符要把参数类型补全上
         */
//        IEat iEat3 = () ->{System.out.println("eat apple and banana!"); };
//        IEat iEat3 = () -> System.out.println("eat apple and banana!"); ;
//        iEat3.eat();
//        IEat iEat3 = (String str) -> System.out.println("eat..." + str);
//        IEat iEat3 = (str) -> System.out.println("eat..." + str);
/*        IEat iEat3 = (str,name) ->{
            System.out.println("eat apple and banana!");
            System.out.println(name);
        };
        iEat3.eat("apple","xiaoming");*/
        //可以带返回值
        IEat iEat3 = (str,name) ->{
            System.out.println("eat apple and banana!");
            return 10;
        };
        //带返回值只有一句代码
        IEat iEat4 = (str,name) ->10;
        iEat3.eat("apple","xiaoming");

        Student[] students = {
                new Student("张三",18),
                new Student("李四",21),
                new Student("王五",19)
        };
        Arrays.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getAge() - o2.getAge();
            }
        });
        //用Lambda
//        Comparator<Student> c = (o1, o2) -> o1.getAge() - o2.getAge();
//        Arrays.sort(students,c);
        Arrays.sort(students,(o1, o2) -> o1.getAge() - o2.getAge());
    }
}

/**
 * jdk1.8中默认方法和静态方法都不影响函数接口契约
 */
interface IEat{
//    public void eat(String str,String name);
    public int eat(String str,String name);
    //默认方法
    public default void print(){
        System.out.println("print test");
    }
    //静态方法
    public static void method(){
        System.out.println("static mehod..");
    }
}
/*
class IEatimpl implements IEat{
    @Override
    public void eat(String str,String name) {
        System.out.println("eat apple");
    }
}*/
